library(tidyverse)

kegg_mva <-
  c('Acat1','Acat2','Hmgcs1','Hmgcs2','Hmgcr','Mvk','Pmvk','Mvd','Idi1','Idi2','Fdps') |>
  str_to_upper()

mva_fct <- tibble(gene = kegg_mva, ordered = fct_inorder(kegg_mva))

gp.xy <- list.files('mission/FPP/post_graphpad/data', full.names = T)

# IFN Hacat --------
ifn.group <- expand_grid(group = c('ctrl','IFN'), time = c(1,3,6,12)) |>
  arrange(time) |>
  pmap_chr(paste,sep = '.')

ifn.group <- c('baseline', ifn.group)

iln <- gp.xy |>
  str_subset('Hacat-IFN') |>
  read_delim(col_names = ifn.group, skip = 1, id = 'gene') |>
  mutate(gene = str_extract(gene, '\\w+(?=.txt)'))

hacat.iln <- iln |>
  pivot_longer(-1) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  write_csv('mission/FPP/post_graphpad/results/iln.hacat.heatmap.csv')

hacat.iln |>
  right_join(mva_fct) |>
  filter(!is.na(baseline)) |>
  arrange(ordered) |>
  select(-c(ordered, contains('ctrl'))) |>
  write_csv('mission/FPP/post_graphpad/results/iln.hacat.mva.heatmap.csv')

# IMQ Hacat ------------
imq.group <- expand_grid(group = c('ctrl','IMQ'), time = c(1,3,6,12)) |>
  arrange(time) |>
  pmap_chr(paste,sep = '.')

imq.group <- c('baseline', imq.group)

imq <- gp.xy |>
  str_subset('Hacat-IMQ') |>
  read_delim(col_names = imq.group, skip = 1, id = 'gene') |>
  mutate(gene = str_extract(gene, '\\w+(?=.txt)'))

imq.tidy <- imq |>
  pivot_longer(-1) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  write_csv('mission/FPP/post_graphpad/results/imq.hacat.heatmap.csv')

imq.tidy |>
  right_join(mva_fct) |>
  filter(!is.na(baseline)) |>
  arrange(ordered) |>
  select(-c(ordered, contains('ctrl'))) |>
  write_csv('mission/FPP/post_graphpad/results/imq.hacat.mva.heatmap.csv')

imq.tidy |>
  column_to_rownames('gene') |>
  pheatmap::pheatmap(cluster_cols = F)

# IMQ mouse ------
mmimq <- gp.xy |>
  str_subset('Hacat',negate = T) |>
  read_delim(col_names = imq.group, skip = 1, id = 'path') |>
  mutate(gene = str_extract(path, '(?<=data/)\\w+'),
         tissue = str_extract(path, '(Der|Epi)'),
         path = NULL)

epi.imq.tidy <- mmimq |>
  filter(tissue == 'Epi') |>
  select(-tissue) |>
  mutate(across(-gene, \(x)x/baseline)) |>
  pivot_longer(-gene) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  write_csv('mission/FPP/post_graphpad/results/epi.imq.heatmap.csv')

epi.imq.tidy |>
  right_join(mva_fct) |>
  filter(!is.na(baseline)) |>
  arrange(ordered) |>
  select(-c(ordered, contains('ctrl'))) |>
  mutate(gene = str_to_title(gene)) |>
  write_csv('mission/FPP/post_graphpad/results/imq.mouse.epi.mva.heatmap.csv')

mmimq |>
  filter(tissue == 'Der') |>
  select(-tissue) |>
  mutate(across(-gene, \(x)x/baseline)) |>
  pivot_longer(-gene) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  right_join(mva_fct) |>
  filter(!is.na(baseline)) |>
  arrange(ordered) |>
  select(-c(ordered, contains('ctrl'))) |>
  mutate(gene = str_to_title(gene)) |>
  write_csv('mission/FPP/post_graphpad/results/imq.mouse.der.mva.heatmap.csv')

## average all ctrl groups
mmimq.1ctrl <- mmimq |>
  mutate(ctrl = mean(c(baseline, ctrl.1, ctrl.3, ctrl.6, ctrl.12)),
         .keep = 'unused')

mmimq.1ctrl |>
  filter(tissue == 'Der') |>
  select(-tissue) |>
  mutate(across(-gene, \(x)x/ctrl)) |>
  pivot_longer(-gene) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  right_join(mva_fct) |>
  filter(!is.na(ctrl)) |>
  arrange(ordered) |>
  select(-c(ordered)) |>
  mutate(gene = str_to_title(gene)) |>
  write_csv('mission/FPP/post_graphpad/results/imq.mouse.der.mva.heatmap.1ctrl.csv')

mmimq.1ctrl |>
  filter(tissue == 'Epi') |>
  select(-tissue) |>
  mutate(across(-gene, \(x)x/ctrl)) |>
  pivot_longer(-gene) |>
  summarise(log2fc = log2(mean(value)), .by = c(gene, name)) |>
  pivot_wider(names_from = name, values_from = log2fc) |>
  right_join(mva_fct) |>
  filter(!is.na(ctrl)) |>
  arrange(ordered) |>
  select(-c(ordered)) |>
  mutate(gene = str_to_title(gene)) |>
  relocate(gene, ctrl) |>
  write_csv('mission/FPP/post_graphpad/results/imq.mouse.epi.mva.heatmap.1ctrl.csv')

# qPCR z score --------------
v3ko_mva <- read_tsv('v3ko_mva.tsv')

v3ko_mva |>
  column_to_rownames('gene') |>
  pheatmap::pheatmap(scale = 'row', cluster_cols = F,
                     cluster_rows = F)

v3ko_mva |>
  pivot_longer(-1) |>
  mutate(zscore = scale(value)[,1], .by = gene) |>
  select(-value) |>
  pivot_wider(names_from = name, values_from = zscore) |>
  write_csv('v3ko_mva_zscore.csv')

v3ko_mva_gapdh <- read_tsv('v3ko_mva_gapdh.tsv')

v3ko_mva_gapdh |>
  pivot_longer(-1) |>
  mutate(zscore = scale(value)[,1], .by = gene) |>
  select(-value) |>
  pivot_wider(names_from = name, values_from = zscore)

v3ko_mva_gapdh |>
  pivot_longer(-1) |>
  left_join(v3ko_mva_gapdh) |>
  mutate(log2fc = value-`PBS-KO`) |>
  select(gene, name, log2fc) |>
  pivot_wider(names_from = name, values_from = log2fc)

v3ko_mva_gapdh |>
  column_to_rownames('gene') |>
  t() |>
  pheatmap::pheatmap(scale = 'column', cluster_cols = F,
                     cluster_rows = F)
